package com.example.mediaandroidjava;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.media3.common.MediaItem;
import androidx.media3.common.Player;
import androidx.media3.common.Timeline;
import androidx.media3.exoplayer.ExoPlayer;
import androidx.media3.exoplayer.source.ShuffleOrder;
import androidx.media3.ui.PlayerView;

import android.os.Bundle;
import android.util.Log;

import com.google.common.collect.ImmutableList;

import java.util.List;

public class PlayerListActivity extends AppCompatActivity {
    private PlayerView playerView;
    private ExoPlayer exoPlayer;
    private static String TAG = "PlayListActivity";
    private static int TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED = 1000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_player_list_activity);
        playerView = findViewById(R.id.player_view_player_list);
        ///多视频列表
        MediaItem mediaItemFirst = MediaItem.fromUri("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4");
        MediaItem mediaItemSecond = MediaItem.fromUri("http://vjs.zencdn.net/v/oceans.mp4");
        exoPlayer = new ExoPlayer.Builder(this).build();
        exoPlayer.addMediaItem(mediaItemFirst);
        exoPlayer.addMediaItem(mediaItemSecond);

        ///调整播放列表
        // 在位置1处添加一个媒体
        exoPlayer.addMediaItem(/* index= */ 1, MediaItem.fromUri("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"));
        // 将位置2处的媒体移到首位
        exoPlayer.moveMediaItem(/* currentIndex= */ 2, /* newIndex= */ 0);
        // 将位置0处的媒体移除
        exoPlayer.removeMediaItem(/* index= */ 0);

        ///替代播放列表和清除播放列表
        List<MediaItem> newItems = ImmutableList.of(MediaItem.fromUri("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"), MediaItem.fromUri("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"));
        exoPlayer.setMediaItems(newItems, true);
        exoPlayer.clearMediaItems();

        ///查询播放列表
        exoPlayer.getMediaItemAt(0);
        exoPlayer.getMediaItemCount();
        exoPlayer.getCurrentMediaItem();
        exoPlayer.hasNextMediaItem();
        exoPlayer.getNextMediaItemIndex();

        ///重复模式(REPEAT_MODE_OFF关闭循环播放、REPEAT_MODE_ALL循环整个列表、REPEAT_MODE_ONE循环单个)
        exoPlayer.setRepeatMode(Player.REPEAT_MODE_OFF);
        exoPlayer.setRepeatMode(Player.REPEAT_MODE_ALL);
        exoPlayer.setRepeatMode(Player.REPEAT_MODE_ONE);

        ///随机播放
        exoPlayer.setShuffleOrder(new ShuffleOrder.DefaultShuffleOrder(new int[]{2, 1, 0}, 1));
        exoPlayer.setShuffleModeEnabled(true);

        //MediaItem设置Id
        //MediaItem mediaItem = new MediaItem.Builder().setUri(uri).setMediaId(mediaId).build();
        //MediaItem mediaItem = new MediaItem.Builder().setUri(uri).setTag(metadata).build();


        exoPlayer.addListener(new Player.Listener() {
            //监听播放过渡到另一个媒体
            @Override
            public void onMediaItemTransition(@Nullable MediaItem mediaItem, int reason) {
                Player.Listener.super.onMediaItemTransition(mediaItem, reason);
                @Nullable String metadata = null;
                Log.i(TAG, "过渡");
                if (mediaItem != null && mediaItem.localConfiguration != null) {
                    metadata = (String) mediaItem.localConfiguration.tag;
                }
                //updateUiForPlayingMediaItem(metadata);
            }

            //添加、删除或移动媒体项时，将调用 立即与 .此回调是 即使玩家尚未做好准备，也会调用。
            @Override
            public void onTimelineChanged(@NonNull Timeline timeline, int reason) {
                Player.Listener.super.onTimelineChanged(timeline, reason);
                if (reason == TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED) {
                    //updateUiForPlaylist(timeline);
                }
            }
        });

        playerView.setPlayer(exoPlayer);
        exoPlayer.prepare();
        exoPlayer.play();
    }
}